/**
 
 @Name : LifeGrid
 @Author: 宋学彦
 @Date: 2015年4月3日 
 @Site：
 */
LifeGrid = new Object();
LifeGrid.gridCount = 30; //格子个数(格子为正方形)
LifeGrid.textSize = 10;
LifeGrid.textRow = 1;
LifeGrid.textWidth = 30;
LifeGrid.canvasWidth;
LifeGrid.canvasHeight;
LifeGrid.gridWidth;
LifeGrid.cellWith;	//单元格的宽度
LifeGrid.canvas;
LifeGrid.colors;
LifeGrid.color;
LifeGrid.text;


LifeGrid.draw = function(birth,start,end,text){
    this.color = "#000000";
    this.drawText(text,10,10);
    this.fillGrid(birth,start,end);
    this.drawGrid();
}

LifeGrid.init = function(canvas){
  var width = window.screen.width;
  if(width>480){
      width = 600;
  }
  this.canvasWidth = width;  //canvas的宽度
  this.canvasHeigh = width+this.textWidth;
  canvas.width = this.canvasWidth;
  canvas.height = this.canvasHeigh;
  this.canvas = canvas;
  this.gridWidth = width;
  this.cellWith = Math.ceil(this.gridWidth/30);
  this.colors = new Array("#000079","#AE0000","#930093","#921AFF","#00CACA","#00A600","#8CEA00","#FFD306","#F75000","#984B4B","#AE57A4");
} 


LifeGrid.drawText = function(text,x,y){  
  var ctx = this.canvas.getContext("2d");
  //ctx.fillStyle = "#000000"; //"#0000ff";
  var textLength = text.length;
  var textRowNum = Math.floor(this.gridWidth/this.textSize);  //单行文本的字数
  this.textRow = Math.ceil(textLength/textRowNum);  //文本行数
  ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
  //this.textSize = ctx.font.match(/\d+/g)[0];
  for (i=0; i <=this.textRow;i++) {
    var textTemp = text.substr(i*textRowNum,textRowNum);
    ctx.fillText(textTemp,x,y+this.textSize*(i+1));
  }
  
}


/*
@function:计算行列和长度
@Parameter:birth:出生年月。start:，事件开始年月。end:事件持续年月
@Date format:YYYY-MMM
@exampe:calcLength("1991-03","2014-11","201504");
*/
LifeGrid.fillGrid = function(birth,start,end){	


    var monthStart = this.calcMouth(birth,start);   
    var monthLength = this.calcMouth(start,end);  
    var colStart = monthStart%this.gridCount - 1;       
    var rowStart = (monthStart-colStart)/this.gridCount - 1;   
    var colLeft= this.gridCount - colStart;   
    if(monthLength <= colLeft){
        this.fillRect(rowStart,colStart,1,monthLength);
    }else{
        this.fillRect(rowStart,colStart,1,colLeft);
        colLeft = monthLength - colLeft;
      if(colLeft <= this.gridCount){
        this.fillRect(rowStart+1,0,1,colLeft);
      }else{
        colTemp = colLeft%this.gridCount;
        rowTemp = (colLeft - colTemp)/this.gridCount;
        this.fillRect(rowStart+1,0,rowTemp,this.gridCount);
        this.fillRect(rowStart+1+rowTemp,0,1,colTemp);
      }
    }
}

/*
@function:计算两个时间之间的月份
@Parameter:start:开始年月。end:结束年月
@Date format:YYYY-MMM
@return: 数字
*/
LifeGrid.calcMouth = function(start,end){
   var yearStart = Number(start.substr(0,4));
   var yearEnd = Number(end.substr(0,4));
   var monthStart = Number(start.substr(5));
   var monthEnd=  Number(end.substr(5));
   var month = Math.floor(12-monthStart);
   var year = Math.floor(yearEnd-yearStart-1); //yearEnd.sub(yearStart.add(1));
   var mouthCount = 0;
   if(year>0){
      mouthCount = year*12+month+monthEnd;
   }else{
      mouthCount = month+monthEnd;
   }
   return mouthCount;
}

/*
@function:画网格
*/
LifeGrid.drawGrid = function(){
   var context = this.canvas.getContext("2d");
   context.beginPath();
   

   var i=0;
   for(i=0;i<this.gridCount;i++){
      context.moveTo(0,this.textWidth+i*this.cellWith);
      context.lineTo(this.cellWith*this.gridCount,this.textWidth+i*this.cellWith);
   }
   for(i=0;i<this.gridCount;i++){
      context.moveTo(i*this.cellWith,this.textWidth);
      context.lineTo(i*this.cellWith,this.textWidth+this.cellWith*this.gridCount);
   }
   context.stroke();
}

//填充多行fillRect(x,y,width,height);
LifeGrid.fillRect = function(row,cell,rowCount,cellCount){
  var ctx=this.canvas.getContext("2d");
  var colorIndex = Math.floor(Math.random() * (10));
  ctx.fillStyle = this.colors[colorIndex]; 
  ctx.fillRect(cell*this.cellWith,this.textWidth+row*this.cellWith,cellCount*this.cellWith,rowCount*this.cellWith);
  ctx.fillStyle = "#000000"; 
}

//调用：accAdd(arg1,arg2)
//返回值：arg1加上arg2的精确结果
function accAdd(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2))
    return (arg1 * m + arg2 * m) / m
}
//给Number类型增加一个add方法
Number.prototype.add = function(arg) {
    return accAdd(arg, this);
}

//调用：accSub(arg1,arg2)
//返回值：arg1减去arg2的精确结果
function accSub(arg1, arg2) {
    var r1, r2, m, n;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
//给Number类型增加一个sub方法，调用起来更加方便。
Number.prototype.sub = function(arg) {
    return accSub(this, arg);
}
